package bag.dao.impl;

import bag.c3p0Untils.C3p0Until;
import bag.c3p0Untils.PageBean;
import bag.dao.ProductDao;
import bag.domain.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by 冷丶 on 2017/10/24.
 */
public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> findAll() {
        QueryRunner runner = new QueryRunner(C3p0Until.getDs());
        String sql = "select * from product";
        try {
            List list = (List) runner.query(sql,new BeanListHandler(Product.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateById(Product product) {
        QueryRunner runner = new QueryRunner(C3p0Until.getDs());
        String sql = "update product set pname = ? ,market_price = ?,shop_price = ?,pdesc=? where pid = ?";
        int count = 0;
            try {
                count = runner.update(sql,product.getPname(),product.getMarket_price(),product.getShop_price(),product.getPdesc(),product.getPid());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        return count;
    }

    @Override
    public Product findById(String pid) {
        QueryRunner runner = new QueryRunner(C3p0Until.getDs());
        String sql = "select * from product where pid =?";
        Product product = null;
        try {
            product = (Product) runner.query(sql,new BeanHandler(Product.class),pid);
            return product;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteById(String pid) throws SQLException {
        QueryRunner runner = new QueryRunner(C3p0Until.getDs());
        String sql = "delete from product where pid = ?";
        int num = runner.update(sql,pid);
        if(num > 0)
        {
            return true;
        }
        else
            return false;
    }
    public int insert(Product product) throws SQLException {
        QueryRunner runner=new QueryRunner(C3p0Until.getDs());
        String sql="insert into product(pname,market_price,shop_price,pdesc) values(?,?,?,?)";
        int count =  runner.update(sql,product.getPname(),product.getMarket_price(),product.getShop_price(),product.getPdesc());
        return count;
    }

    @Override
    public List<Product> findByCondition(String condition) {
        QueryRunner runner = new QueryRunner(C3p0Until.getDs());
        String sql = "select * from product where pname like ?";
        String temp = "%"+condition+"%";
        try {
            List list = (List) runner.query(sql,new BeanListHandler(Product.class),temp);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageBean<Product> findPage(int currentPage, int pageSize) {
        PageBean pageBean = new PageBean();
        pageBean.setCurrenPage(currentPage);
        pageBean.setPageSize(pageSize);
        QueryRunner runner = new QueryRunner(C3p0Until.getDs());
        String sql = "select * from product order by pid limit ?,?";
        String countSql = "select count(*) from product";
        try {
            List<Product> list = (List<Product>) runner.query(sql,new BeanListHandler(Product.class),(currentPage-1)*10,pageSize);
            pageBean.setPageList(list);
            Long c = (Long) runner.query(countSql,new ScalarHandler());
            int count = c.intValue();
            pageBean.setCount(count);
            pageBean.getPageCount();
            return pageBean;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}
